window.define(["jquery"], function ($) {
    //var basePath = "";
    var basePathTest = "assets/mock";
    var serversCommon={
        getReqOption:function(params){
            var option={
                url:params[0],
                param:params[1]||{},
                successCallback:params[2],
                isLoading:params[3],
                token:null
            };
            option.url=$.ExtAssist.urlParam.getNewUrl(option.url,false);
            return option;
        },
        getMapInfo:function(type,id){
            var strId=id.toString();
            return dataMapping[type].hasOwnProperty(strId)?dataMapping[type][strId]:{idMap:strId}
        },
        getMapKey:function(type,id){
            var idKey="";
            var strId=(typeof id==="undefined" || id===null)?"":id.toString();
            if(strId.length>0){
                var json=dataMapping[type];
                for(var k in json){
                    if(json.hasOwnProperty(k)){
                        if(json[k].idMap===strId){
                            idKey= k.toString();
                            break;
                        }
                    }
                }
            }
            return idKey.length===0?strId:idKey;
        },
        getListMap:function(res,type){
            var list=res.success?res.data:[];
            for(var i=0;i<list.length;i++){
                var info =list[i];
                var id=info.id;
                var mInfo=this.getMapInfo(type,id);
                info.idMap=mInfo.idMap;
                info.mapInfo=mInfo;
            }
            return list;
        }
    };
    var dataMapping={
        crowdType:{"CUSTOMER_FILE":{idMap:"1",name:"自定义人群"},"LOOKALIKE":{idMap:"2",name:"相似人群"}},
        customerStatus:{"-1":{idMap:"-1",name:"审核不通过"},"0":{idMap:"0",name:"待提交"},"1":{idMap:"1",name:"待审核"},"2":{idMap:"2",name:"审核通过"},"3":{idMap:"3",name:"冻结"}},
        spreadType:{"1":{idMap:"1"},"2":{idMap:"2"}},
        objMatterType:{"1":{idMap:"1",filterType:"LINK"},"2":{idMap:"2",filterType:"ANDROID"},"3":{idMap:"3",filterType:"IOS"},"4":{idMap:"4",filterType:"WECHAT"}}
    };
    var servers = {
        getPageParamMap:function(){
            return {offset:"offset",limit:"limit",search:"search"}
        },
        getSendSuccess:function (data) {
            return {success:true,data:data,dataOriginal:data}
        },
        getSendError:function (msg) {
            return {success:false,msg:msg}
        }
    };
    servers.base={
        industry: function (cb) {
            var options=serversCommon.getReqOption([basePathTest + "/base/Industry.json", {}, cb]);
            $.ExtAjax.initGetSimple(options);
        },
        crowdBag: function (cb) {
            var options=serversCommon.getReqOption([basePathTest + "/base/crowdBag.json", {}, cb]);
            $.ExtAjax.initGetSimple(options);
        },
        spreadType: function (cb) {
            var options=serversCommon.getReqOption([basePathTest + "/base/spreadType.json", {}, cb]);
            $.ExtAjax.initGetSimple(options);
        },
        objMatterType: function (cb) {
            var options=serversCommon.getReqOption([basePathTest + "/base/objMatterType.json", {}, cb]);
            $.ExtAjax.initGetSimple(options);
        },
        channelPack: function (param,cb) {
            var options=serversCommon.getReqOption([basePathTest + "/base/channelPack.json", {}, cb]);
            $.ExtAjax.initGetSimple(options);
        },
        resourceType: function (param,cb) {
            var options=serversCommon.getReqOption([basePathTest + "/base/resourceType.json",param, cb]);
            $.ExtAjax.initGetSimple(options);
        },
        getMapCrowdType:function (key,isMap) {
            isMap=isMap||false;
            if(isMap){
                return serversCommon.getMapKey("crowdType",key);
            }else{
                return serversCommon.getMapInfo("crowdType",key);
            }
        },
        getMapCustomerStatus:function (key,isMap) {
            isMap=isMap||false;
            if(isMap){
                return serversCommon.getMapKey("customerStatus",key);
            }else{
                return serversCommon.getMapInfo("customerStatus",key);
            }
        },
        getMapSpreadType:function (key,isMap) {
            isMap=isMap||false;
            if(isMap){
                return serversCommon.getMapKey("spreadType",key);
            }else{
                return serversCommon.getMapInfo("spreadType",key);
            }
        },
        getMapObjMatterType:function (key,isMap) {
            isMap=isMap||false;
            if(isMap){
                return serversCommon.getMapKey("objMatterType",key);
            }else{
                return serversCommon.getMapInfo("objMatterType",key);
            }
        },
        uploadImg:basePathTest + "/base/Save.json",
        uploadFile:basePathTest + "/base/SaveFile.json"
    };
    servers.account = {
        dataInfo:null,
        //dataToken:null,
        info:function(cb){
            var self=this;
            if(!$.ExtCommon.isCheckEmpty(self.dataInfo)){
                cb(self.dataInfo)
            }else{
                var options=serversCommon.getReqOption([basePathTest + "/account/info.json", {}, function(res){
                    var newJson={};
                    if(res.success){
                        newJson= $.ExtCommon.getMappingJson(res.data, {
                            "userId":"userId","userName":"name","authorize":"permissions","roleName":"role"
                        },true);
                        $.cookie('usertoken',"1");
                    }
                    res.data=newJson;
                    self.dataInfo=res;
                    cb(self.dataInfo);
                }]);
                $.ExtAjax.initGetSimple(options);
            }
        },
        login: function (param, cb) {
            var self=this;
            var options=serversCommon.getReqOption([basePathTest + "/account/login.json", param, function(res){
                if(res.success){
                    self.dataInfo=null;
                    self.info(function(resInfo){
                        cb(resInfo)
                    });
                }else{
                    cb(res)
                }
            },false]);
            $.ExtAjax.initPostSimple(options);
        },
        logout: function (cb) {
            var self=this;
            var options=serversCommon.getReqOption([basePathTest + "/account/logout.json", {}, function(res){
                if(res.success){
                    self.dataToken=null;
                    self.dataInfo=null;
                    $.cookie('usertoken',false);
                }
                cb(res)
            }]);
            $.ExtAjax.initPostSimple(options);
        },
        check: function (cb) {
            this.info(function(resInfo){
                cb(resInfo)
            });
        },
        pwd: function (data, cb) {
            var options=serversCommon.getReqOption([basePathTest + "/account/Save.json", data, cb]);
            $.ExtAjax.initPutSimple(options);
        },
        pagePower:function(param,cb){
            var options=serversCommon.getReqOption([basePathTest + "/account/permission.json", param, cb]);
            $.ExtAjax.initGetSimple(options);
        }
    };
    servers.customer={
        pageList: function (param,cb) {
            var options=serversCommon.getReqOption([basePathTest + "/customer/ListPage.json", param, cb]);
            $.ExtAjax.initGetPageData(options);
        },
        list:function (cb) {
            var options=serversCommon.getReqOption([basePathTest + "/customer/List.json", {}, cb]);
            $.ExtAjax.initGetSimple(options);
        },
        info: function (id, cb) {
            var options=serversCommon.getReqOption([basePathTest + "/customer/Info.json", {}, cb]);
            $.ExtAjax.initGetSimple(options);
        },
        add: function (data, cb) {
            var options=serversCommon.getReqOption([basePathTest + "/customer/Save.json", data, cb]);
            $.ExtAjax.initPostSimple(options);
        },
        update: function (data, cb) {
            var options=serversCommon.getReqOption([basePathTest + "/customer/Save.json", data, cb]);
            $.ExtAjax.initPostSimple(options);
        }
    };
    servers.agent={
        pageList: function (param,cb) {
            var options=serversCommon.getReqOption([basePathTest + "/agent/ListPage.json", param, cb]);
            $.ExtAjax.initGetPageData(options);
        },
        list:function (cb) {
            var options=serversCommon.getReqOption([basePathTest + "/agent/List.json", {}, cb]);
            $.ExtAjax.initGetSimple(options);
        },
        customerNum: function (param, cb) {
            var options=serversCommon.getReqOption([basePathTest + "/agent/customerNum.json", param, cb]);
            $.ExtAjax.initGetSimple(options);
        },
        info: function (param, cb) {
            var options=serversCommon.getReqOption([basePathTest + "/agent/Info.json", param, cb]);
            $.ExtAjax.initGetSimple(options);
        },
        add: function (data, cb) {
            var options=serversCommon.getReqOption([basePathTest + "/agent/Save.json", data, cb]);
            $.ExtAjax.initPostSimple(options);
        },
        update: function (data, cb) {
            var options=serversCommon.getReqOption([basePathTest + "/agent/Save.json", data, cb]);
            $.ExtAjax.initPostSimple(options);
        }
    };
    servers.campaign={
        pageList: function (param,cb) {
            var options=serversCommon.getReqOption([basePathTest + "/campaign/PageList.json", param, cb]);
            $.ExtAjax.initGetPageData(options);
        },
        info: function (id, cb) {
            var options=serversCommon.getReqOption([basePathTest + "/campaign/Info.json", {}, cb]);
            $.ExtAjax.initGetSimple(options);
        },
        add: function (data, cb) {
            var options=serversCommon.getReqOption([basePathTest + "/campaign/Save.json", data, cb]);
            $.ExtAjax.initPostSimple(options);
        },
        update: function (data, cb) {
            var options=serversCommon.getReqOption([basePathTest + "/campaign/Save.json", data, cb]);
            $.ExtAjax.initPostSimple(options);
        }
    };
    servers.spread={
        pageList: function (campaignId,param,cb) {
            var options=serversCommon.getReqOption([basePathTest + "/spread/ListPage.json", param, cb]);
            $.ExtAjax.initGetPageData(options);
        },
        list:function (param,cb) {
            var options=serversCommon.getReqOption([basePathTest + "/spread/List.json", {}, cb]);
            $.ExtAjax.initGetSimple(options);
        },
        info: function (campaignId,param, cb) {
            var options=serversCommon.getReqOption([basePathTest + "/spread/Info.json", {}, cb]);
            $.ExtAjax.initGetSimple(options);
        },
        add: function (campaignId,data, cb) {
            var options=serversCommon.getReqOption([basePathTest + "/spread/Save.json", data, cb]);
            $.ExtAjax.initPostSimple(options);
        },
        update: function (campaignId,data, cb) {
            var options=serversCommon.getReqOption([basePathTest + "/spread/Save.json", data, cb]);
            $.ExtAjax.initPostSimple(options);
        },
        updateStatus: function (campaignId,data, cb) {
            var options=serversCommon.getReqOption([basePathTest + "/spread/Save.json", data, cb,false]);
            $.ExtAjax.initPostSimple(options);
        },
        updatePrice: function (campaignId,data, cb) {
            var options=serversCommon.getReqOption([basePathTest + "/spread/Save.json", data, cb,false]);
            $.ExtAjax.initPostSimple(options);
        }
    };
    servers.strategy={
        pageList: function (campaignId, param, cb) {
            var options=serversCommon.getReqOption([basePathTest + "/strategy/ListPage.json", param, cb]);
            $.ExtAjax.initGetPageData(options);
        },
        info: function (param, cb) {
            var options=serversCommon.getReqOption([basePathTest + "/strategy/Info.json", {}, cb]);
            $.ExtAjax.initGetSimple(options);
        },
        add: function (campaignId,data, cb) {
            var options=serversCommon.getReqOption([basePathTest + "/strategy/Save.json", data, cb]);
            $.ExtAjax.initPostSimple(options);
        },
        update: function (campaignId,data, cb) {
            var options=serversCommon.getReqOption([basePathTest + "/strategy/Save.json", data, cb]);
            $.ExtAjax.initPostSimple(options);
        },
        updateStatus: function (campaignId, data, cb) {
            var options=serversCommon.getReqOption([basePathTest + "/strategy/Save.json", data, cb,false]);
            $.ExtAjax.initPostSimple(options);
        },
        updatePrice: function (campaignId, data, cb) {
            var options=serversCommon.getReqOption([basePathTest + "/strategy/Save.json", data, cb,false]);
            $.ExtAjax.initPostSimple(options);
        }
    };
    servers.creative={
        pageList: function (param,cb) {
            var options=serversCommon.getReqOption([basePathTest + "/creative/ListPage.json", param, cb]);
            $.ExtAjax.initGetPageData(options);
        },
        modalList: function (campaignId,param,cb) {
            var options=serversCommon.getReqOption([basePathTest + "/creative/ListModal.json", param, cb]);
            $.ExtAjax.initGetPageData(options);
        },
        info: function (id, cb) {
            var options=serversCommon.getReqOption([basePathTest + "/creative/Info.json", {}, cb]);
            $.ExtAjax.initGetSimple(options);
        },
        strategyList:function(param,cb) {
            //var options=serversCommon.getReqOption([basePathTest + "/creative/selectCreative", param, cb]);
            $.ExtAjax.initGetPageData(options);
        },
        add: function (data, cb) {
            var options=serversCommon.getReqOption([basePathTest + "/creative/Save.json", data, cb]);
            $.ExtAjax.initPostSimple(options);
        },
        update: function (data, cb) {
            var options=serversCommon.getReqOption([basePathTest + "/creative/Save.json", data, cb]);
            $.ExtAjax.initPostSimple(options);
        },
        updateStatus: function (data, cb) {
            var options=serversCommon.getReqOption([basePathTest + "/creative/Save.json", data, cb,false]);
            $.ExtAjax.initPostSimple(options);
        }
    };
    servers.media={
        list:function (cb) {
            var options=serversCommon.getReqOption([basePathTest + "/media/List.json", {}, cb]);
            $.ExtAjax.initGetSimple(options);
        }
    };
    servers.space={
        modalList: function (campaignId,param,cb) {
            var options=serversCommon.getReqOption([basePathTest + "/space/ListModal.json", param, cb]);
            $.ExtAjax.initGetPageData(options);
        },
        materialInfo:function (id,cb) {
            var options=serversCommon.getReqOption([basePathTest + "/space/Material.json", {}, cb]);
            $.ExtAjax.initGetSimple(options);
        }
    };
    servers.targetPack={
        pageList: function (param,cb) {
            var options=serversCommon.getReqOption([basePathTest + "/targetPack/List.json", param, cb]);
            $.ExtAjax.initGetPageData(options);
        },
        list:function (param,cb) {
            var options=serversCommon.getReqOption([basePathTest + "/targetPack/List.json", {}, cb]);
            $.ExtAjax.initGetSimple(options);
        },
        strategyList:function(id,cb) {
            var options=serversCommon.getReqOption([basePathTest + "/targetPack/ListStrategy.json", {}, cb]);
            $.ExtAjax.initGetPageData(options);
        },
        info: function (id, cb) {
            var options=serversCommon.getReqOption([basePathTest + "/targetPack/Info.json", {}, cb]);
            $.ExtAjax.initGetSimple(options);
        },
        add: function (data, cb) {
            var options=serversCommon.getReqOption([basePathTest + "/targetPack/Save.json", data, cb]);
            $.ExtAjax.initPostSimple(options);
        },
        update: function (data, cb) {
            var options=serversCommon.getReqOption([basePathTest + "/targetPack/Save.json", data, cb]);
            $.ExtAjax.initPostSimple(options);
        },
        delete: function (id, cb) {
            var options=serversCommon.getReqOption([basePathTest + "/targetPack/Save.json", {}, cb]);
            $.ExtAjax.initPostSimple(options);
        }
    };
    servers.targets={
        area:function (param,cb) {
            var options=serversCommon.getReqOption([basePathTest + "/common/regionStruct", {}, cb]);
            $.ExtAjax.initGetSimple(options);
        },
        interest:function (cb) {
            var options=serversCommon.getReqOption([basePathTest + "/targets/Interest.json", {}, cb]);
            $.ExtAjax.initGetSimple(options);
        },
        crowd:function (cb) {
            var options=serversCommon.getReqOption([basePathTest + "/targets/Crowd.json", {}, cb]);
            $.ExtAjax.initGetSimple(options);
        },
        device:function (cb) {
            var options=serversCommon.getReqOption([basePathTest + "/targets/Device.json", {}, cb]);
            $.ExtAjax.initGetSimple(options);
        },
        appAction:function (cb) {
            var options=serversCommon.getReqOption([basePathTest + "/targets/AppCategory.json", {}, cb]);
            $.ExtAjax.initGetSimple(options);
        }
    };
    servers.crowd={
        pageList: function (param,cb) {
            var options=serversCommon.getReqOption([basePathTest + "/crowd/PageList.json", param, cb]);
            $.ExtAjax.initGetPageData(options);
        },
        customList: function (cb) {
            var options=serversCommon.getReqOption([basePathTest + "/crowd/List.json", {}, cb]);
            $.ExtAjax.initGetPageData(options);
        },
        customInfo: function (param, cb) {
            var options=serversCommon.getReqOption([basePathTest + "/crowd/InfoCustom.json", {}, cb]);
            $.ExtAjax.initGetSimple(options);
        },
        addCustom: function (data, cb) {
            var options=serversCommon.getReqOption([basePathTest + "/crowd/Save.json", data, cb]);
            $.ExtAjax.initPostSimple(options);
        },
        updateCustom: function (data, cb) {
            var options=serversCommon.getReqOption([basePathTest + "/crowd/Save.json", data, cb]);
            $.ExtAjax.initPostSimple(options);
        },
        delete: function (param, cb) {
            var options=serversCommon.getReqOption([basePathTest + "/crowd/Save.json", {}, cb]);
            $.ExtAjax.initPostSimple(options);
        },
        similarInfo: function (param, cb) {
            var options=serversCommon.getReqOption([basePathTest + "/crowd/InfoSimilar.json", {}, cb]);
            $.ExtAjax.initGetSimple(options);
        },
        addSimilar: function (data, cb) {
            var options=serversCommon.getReqOption([basePathTest + "/crowd/Save.json", data, cb]);
            $.ExtAjax.initPostSimple(options);
        },
        updateSimilar: function (data, cb) {
            var options=serversCommon.getReqOption([basePathTest + "/crowd/Save.json", data, cb]);
            $.ExtAjax.initPostSimple(options);
        }
    };
    servers.report={
        pageList: function (param,cb) {
            var options=serversCommon.getReqOption([basePathTest + "/report/List.json", param, cb]);
            $.ExtAjax.initGetPageData(options);
        },
        reportCity: function (param,cb) {
            var options=serversCommon.getReqOption([basePathTest + "/report/Area.json", param, cb]);
            $.ExtAjax.initGetSimple(options);
        },
        reportGender: function (param,cb) {
            var options=serversCommon.getReqOption([basePathTest + "/report/Gender.json", param, cb]);
            $.ExtAjax.initGetSimple(options);
        },
        reportAge:function (param,cb) {
            var options=serversCommon.getReqOption([basePathTest + "/report/Age.json", param, cb]);
            $.ExtAjax.initGetSimple(options);
        },
        campaignHour: function (param,cb) {
            var options=serversCommon.getReqOption([basePathTest + "/report/Hour.json", param, cb]);
            $.ExtAjax.initGetSimple(options);
        },
        campaignDay: function (param,cb) {
            var options=serversCommon.getReqOption([basePathTest + "/report/Day.json", param, cb]);
            $.ExtAjax.initGetSimple(options);
        },
        strategyHour: function (param,cb) {
            var options=serversCommon.getReqOption([basePathTest + "/report/Hour.json", param, cb]);
            $.ExtAjax.initGetSimple(options);
        },
        strategyDay: function (param,cb) {
            var options=serversCommon.getReqOption([basePathTest + "/report/Day.json", param, cb]);
            $.ExtAjax.initGetSimple(options);
        },
        //down
        downHour:function(param){
            window.location.href=basePathTest + "/report/info.json?"+param;
        },
        downDay:function(param){
            window.location.href=basePathTest + "/report/info.json?"+param;
        },
        downloadStrategyGroup:function(param){
            window.location.href=basePathTest + "/report/info.json?"+param;
        },
        downloadStrategy:function(param){
            window.location.href=basePathTest + "/report/info.json?"+param;
        },
        downloadCity:function(param){
            window.location.href=basePathTest + "/report/info.json?"+param;
        },
        downloadGender:function(param){
            window.location.href=basePathTest + "/report/info.json?"+param;
        },
        downloadAge:function(param){
            window.location.href=basePathTest + "/report/info.json?"+param;
        }
    };
    //用户
    servers.user={
        pageList: function (param,cb) {
            var options=serversCommon.getReqOption([basePathTest + "/user/ListPage.json", param, cb]);
            $.ExtAjax.initGetPageData(options);
        },
        add: function (data, cb) {
            var options=serversCommon.getReqOption([basePathTest + "/user/Info.json", data, cb]);
            $.ExtAjax.initPostSimple(options);
        },
        update: function (data, cb) {
            var options=serversCommon.getReqOption([basePathTest + "/user/Info.json", data, cb]);
            $.ExtAjax.initPutSimple(options);
        }
    };
    servers.finance={
        agentListTransfer: function (param,cb) {
            var options=serversCommon.getReqOption([basePathTest + "/finance/AgentListTransfer.json", param, cb]);
            $.ExtAjax.initGetPageData(options);
        },
        agentTransferIn: function (data, cb) {
            var options=serversCommon.getReqOption([basePathTest + "/finance/Save.json", data, cb]);
            $.ExtAjax.initPostSimple(options);
        },
        agentTransferOut: function (data, cb) {
            var options=serversCommon.getReqOption([basePathTest + "/finance/Save.json", data, cb]);
            $.ExtAjax.initPostSimple(options);
        },
        agentListStream: function (param,cb) {
            var options=serversCommon.getReqOption([basePathTest + "/finance/AgentListStream.json", param, cb]);
            $.ExtAjax.initGetPageData(options);
        },
        customerListTransfer: function (param,cb) {
            var options=serversCommon.getReqOption([basePathTest + "/finance/CustomerListTransfer.json", param, cb]);
            $.ExtAjax.initGetPageData(options);
        },
        customerListConsume: function (param,cb) {
            var options=serversCommon.getReqOption([basePathTest + "/finance/CustomerListConsume.json", param, cb]);
            $.ExtAjax.initGetPageData(options);
        },
        surplusAmount: function (cb) {
            var options=serversCommon.getReqOption([basePathTest + "/finance/Info.json", {}, cb]);
            $.ExtAjax.initGetSimple(options);
        },
        surplusAmountByCustomer: function (param,cb) {
            var options=serversCommon.getReqOption([basePathTest + "/finance/Info1.json", param, cb]);
            $.ExtAjax.initGetSimple(options);
        }
    };
    return servers;
});